//
//  Pantalla1.m
//  Grues
//
//  Created by Miquel Viladomat on 17/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Pantalla1.h"
#import "ZonaBlavaAppDelegate.h"

@implementation Pantalla1
@synthesize mapView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation {
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.005;
    span.longitudeDelta = 0.005;
    CLLocationCoordinate2D location;
    location.latitude = aUserLocation.coordinate.latitude;
    location.longitude = aUserLocation.coordinate.longitude;
    region.span = span;
    region.center = location;
    [aMapView setRegion:region animated:YES];
    
    ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
    dataCenter.latv=[NSString stringWithFormat:@"%f",aUserLocation.coordinate.latitude];
    dataCenter.longv=[NSString stringWithFormat:@"%f",aUserLocation.coordinate.longitude];
    NSLog([NSString stringWithFormat:@"%@ / %@",dataCenter.latv,dataCenter.longv]);
    
    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:aUserLocation.coordinate];
	[geocoder setDelegate:self];
	[geocoder start];


}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
    
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
    //NSLog(@"The geocoder has returned: %@", [placemark addressDictionary]);
    ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
    dataCenter.posiciov=[NSString stringWithFormat:@"%@",[placemark.addressDictionary objectForKey:@"Street"]];
    //NSLog(@"The geocoder has returned: %@", [placemark.addressDictionary objectForKey:@"Street"]);
   
}

- (void)viewDidLoad
{
    self.mapView.mapType=MKMapTypeStandard;
    self.mapView.showsUserLocation=YES;
    mapView.delegate = self;

       [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc{
    [super dealloc];
    [mapView release];
}

@end
